/*
 * $Id: NewAssetRegistrationController.java,v 1.5 2012/05/21 16:15:40 l501902876 Exp $
 * 
 * Copyright (C) 2010 General Electric Company. All Rights Reserved.
 * 
 * This software is the confidential and proprietary information of the General
 * Electric Company (GE). You shall not disclose this software and shall use it
 * only in accordance with the terms of the license agreement you entered into
 * with GE.
 * 
 * GE MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE
 * SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR
 * NON-INFRINGEMENT. GE SHALL NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE
 * AS A RESULT OF USING, MODIFYING, OR DISTRIBUTING THIS SOFTWARE OR ITS
 * DERIVATIVES.
 */

package com.ge.healthcare.autosc.ui.assetReg.web;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.validation.BindException;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.SimpleFormController;

import com.ge.healthcare.autosc.common.ASCLogger;
import com.ge.healthcare.autosc.common.ASCUtil;
import com.ge.healthcare.autosc.ui.assetReg.domain.NewAssetReObject;
import com.ge.healthcare.autosc.ui.assetReg.domain.SblAssetRegFailRec;
import com.ge.healthcare.autosc.ui.assetReg.service.FailureRecordManager;
import com.ge.healthcare.autosc.ui.assetReg.service.AssetRegistrationHelper.RegistrationType;


/**
 * This controller handles the manual registration of a failure record
 * @author lakshmi
 *
 */
public class NewAssetRegistrationController extends SimpleFormController {
	FailureRecordManager failureRecordMananger;

	public FailureRecordManager getFailureRecordMananger() {
		return failureRecordMananger;
	}
	public void setFailureRecordMananger(
			FailureRecordManager failureRecordMananger) {
		this.failureRecordMananger = failureRecordMananger;
	}
	/**
	 * This function help in loading the connection type data in the html select boxes. 
	 * Values can be added here or can be picked from database accordingly.
	 * @param request
	 * @return
	 * @throws Exception
	 */
	protected Map referenceData(HttpServletRequest request) throws Exception {
    	Map<Object, Object> dataMap = new HashMap<Object, Object>();
		
		List<String> connTypes = new ArrayList<String>();
		connTypes.add("Insite1");
		connTypes.add("Insite EXC");
    	dataMap.put("connectionTypes",connTypes);
    	
    	return dataMap;
    }
	/**
	 * This method handles the registration of the new failure record
	 * @param request
	 * @param response
	 * @param command
	 * @param errors
	 * @return
	 * @throws Exception
	 */
	protected ModelAndView onSubmit(HttpServletRequest request,
			HttpServletResponse response, Object command, BindException errors)
			 throws Exception{
		
		NewAssetReObject assetReObject=(NewAssetReObject)command;
		ASCLogger.info(this.getClass(), "onSubmit", "Received assetReObject:" + assetReObject);
		Map<String, Object> myModel = new HashMap<String, Object>();
		String registerRequestStatus = null;
		//get the system id from the user form data
		String reRegisterSysId = assetReObject.getSystemId();
		if (reRegisterSysId != null) {
	//perform the register and remove record for this system id
			try {
				SblAssetRegFailRec record = new SblAssetRegFailRec();
				record.setSystemId(reRegisterSysId);
				record.setConnType(assetReObject.getConnectionType());
				if (!ASCUtil.isNullOrEmpty(assetReObject.getFailureDate())) {
					record.setFailureDate(new SimpleDateFormat(
							"yyyy/MM/dd").parse(assetReObject.getFailureDate()));
				} else {
					record.setFailureDate(new Date());
				}
				if (!ASCUtil.isNullOrEmpty(assetReObject.getContextId())) {
					record.setRemoteAccessInfo(assetReObject.getContextId() + "<SEP>"
							+ assetReObject.getContextName());
				}				
				failureRecordMananger.registerAndRemoveRecord(record,RegistrationType.NewAssetRegistration);
				registerRequestStatus = "Registration Successful for SystemId = "
					+ reRegisterSysId + " and Record Removed from Failure Table";
				ASCLogger.info(this.getClass(), "onSubmit", "registerRequestStatus:" + registerRequestStatus);
			} catch (Exception e) {
				registerRequestStatus = "Registration Failed for SystemId = "
					+ reRegisterSysId+" Error Msg: "+e.getMessage();
				request.setAttribute("regFail", true);
				ASCLogger.error(this.getClass(), "onSubmit", "Failed Registration for systemId:" + reRegisterSysId, e);
			}
			
		}
	//send the response to UI
		myModel.put("registerRequestStatus", registerRequestStatus);

		return new ModelAndView("newAssetRegister", "model", myModel);
	
	}

}
